'use strict';

const Controller = require('egg').Controller;

class NewsController extends Controller {
    async news() {
        const { app, ctx, service } = this;
        const payload = {
            data: {
                title: app.super
            }
        };
        await ctx.render('news/news.html', payload);
    }

    async getNewsList() {
        const { app, ctx, service } = this;
        const data = await service.news.getNewsSrv();
        ctx.body = {
            status: 0,
            msg: app.super,
            date: ctx.helper.formatDate(Date.now()),
            data
        };
    }

    async getNews() {
        const { app, ctx, logger } = this;
        const {id} = ctx.params;
        ctx.body = {
            status: 0,
            date: ctx.helper.formatDate(Date.now()),
            msg: `get news list hello: ${id}`
        };
    }

    async getNewsMovie() {
        const { app, ctx, logger } = this;

        const result = await app.model.User.createTable();
        ctx.body = {
            status: 0,
            date: result,
            msg: `getNewsMovie`
        };
    }

    async testWork() {
        const { app, ctx } = this;
        const { body } = ctx.request;
        const { event, birthday, id } = body;

        let result = {};
        switch (Number(event)) {
            case 1:
                result = await app.model.Test.create({
                    birthday
                });
                break;

            case 2:
                result = await app.model.Test.findAll({
                    where: {

                    },
                    order: [['created_at', 'desc']],
                    limit: 1,
                    offset: 0
                });
                break;

            case 3:
                result = await app.model.Test.findOne({
                    where: {
                        id
                    }
                });

                const date = new Date(result.birthday);
                result = {
                    birthday: result.birthday,
                    local: `${date.getFullYear()} ${date.getMonth() + 1} ${date.getDate()}`
                };
                break;
        }

        ctx.successResult(result);
    }
}

module.exports = NewsController;
